/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package downloadimages;

import java.io.FileNotFoundException;
import org.apache.http.client.methods.HttpGet;
import javax.swing.JOptionPane;

/**
 *
 * @author Kubik
 */
public class Downloader {
    String url="";
    java.io.File file = null;
    
    private org.apache.http.impl.client.DefaultHttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient();
    private org.apache.http.HttpResponse httpResponse = null;
    
    public Downloader(){
        this.url = "http://static.ragecollection.com/ragecomics/28811.png";
        
        this.file = new java.io.File("");
    }
    
    public Downloader(String url){
        this.url = url;
    }
    
    public Downloader(String url, java.io.File file){
        this.url = url;
        this.file = file;
    }
    
    public String download(){
        HttpGet httpGet = new HttpGet(url);
        String tmp = null;
        
        try{
            java.io.FileOutputStream out = new java.io.FileOutputStream(file);
        }catch(java.io.FileNotFoundException e){
            JOptionPane.showMessageDialog(null, "FileNotFound Excpetion!");
        }
        
        try{
            httpResponse = httpClient.execute(httpGet);
        }catch(java.io.IOException e){
            JOptionPane.showMessageDialog(null, "Chyba při loadPage-execute! "+e.getMessage());
        }
        try{
            tmp = new String(this.handleResponse(httpResponse));
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Chyba při převodu response na arr! "+e.getMessage());
        }
        httpGet.abort();
        
        return tmp;
    }
    
    public byte[] handleResponse(org.apache.http.HttpResponse response) throws org.apache.http.client.ClientProtocolException, java.io.IOException {
        org.apache.http.HttpEntity entity = response.getEntity();
        if (entity != null) {
            return org.apache.http.util.EntityUtils.toByteArray(entity);
        } else {
            return null;
        }
    }
}
